<?php

namespace app\adminapi\controller;

use think\facade\Db;

class Industry extends Common
{
    public function list()
    {
        $data = $this->_vali([
            'keyword.default' => '',
            'doctimeStart.default' => '',
            'doctimeEnd.default' => '',
        ],"POST");

        $where = [];

        if ($data['keyword']) {
            $where[] = ['name','like',"%".$data['keyword']."%"];
        }

        $list = Db::name('industry')->field('id,name')->where($where)->paginate(10);

        $list = $list->toArray();

        return json(['status'=>200,'sum'=>$list['total'],'list'=>$list['data']]);
    }

    /**
     * 添加/编辑
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function save()
    {
        $data = $this->_vali([
            'id.default' => '',
            'name.require' => '请输地区名',
        ],"POST");

        if ($data['id']) {
            if (Db::name('industry')->where('id','<>',$data['id'])->where('name',$data['name'])->find()) {
                return json(['code'=>0,'msg'=>'角色名称已存在']);
            }
            Db::name('industry')->where('id',$data['id'])->update($data);
        } else {
            if (Db::name('industry')->where('name',$data['name'])->find()) {
                return json(['code'=>0,'msg'=>'角色名称已存在']);
            }
            $data['create_time'] = time();
            if (Db::name('industry')->insert($data)) {
                return json(['code'=>200,'msg'=>'操作成功']);
            } else {
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }

        return json(['code'=>200,'msg'=>'操作成功']);
    }

    /**
     * 获取详情
     * @return \think\response\Json
     */
    public function get()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
        ],"POST");

        $data = Db::name('industry')->where('id',$data['id'])->field('id,name')->find();

        return json(['code'=>200,'data'=>$data]);
    }

    /**
     * 设置状态
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function status()
    {
        $data = $this->_vali([
            'id.require' => 'id不能为空',
            'status.require' => '请输入商户简称',
        ],"POST");

        $res = Db::name('industry')->where('id',$data['id'])->update($data);
        if ($res) {
            return json(['code'=>200,'msg'=>'操作成功']);
        } else {
            return json(['code'=>0,'msg'=>'操作失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del()
    {
        $data = $this->_vali([
            'ids.require' => 'ids不能为空',
        ],"POST");

        if (Db::name('industry')->where('id','in',$data['ids'])->delete()) {
            return json(['code'=>200,'data'=>'操作成功']);
        } else {
            return json(['code'=>200,'data'=>'操作失败']);
        }
    }

    public function getcate(){
        $list = Db::name('industry')->field('id,name')->select();

        $list = $list->toArray();

        return json(['status'=>200,'data'=>$list]);
    }
}